using System;
using System.Collections.Generic;
using System.Data;

namespace ApplicationTemplate
{
    class CreateDamSelectAPI
    {

        public static String CreateGetAll(String realTableName, String modelClassName, DataSet FieldsDs)
        {
            String tableName = ApplcationTemplate.GetTableName(realTableName);
            return String.Format(
@"public DataSet GetAll(DBManager dbManager)
        {{
            string sqlString = string.Format(@""
                SELECT
                    *
                FROM
                    {{0}}
            "", this.{0});

            DbCommand command = GetSqlStringCommand(sqlString);

            //SQL文を実行する
            return ExecuteDataSet(command, dbManager);
        }}
",
            realTableName);
        }

        public static String CreateSelect(String realTableName, String modelClassName, DataSet FieldsDs)
        {
            String tableName = ApplcationTemplate.GetTableName(realTableName);
            return String.Format(
@"public DataSet Get{0}Info({1} model, DBManager dbManager)
        {{
            string sqlString = string.Format(@""
                SELECT
                    *
                FROM
                    {{0}}
                WHERE
{2}
            "", this.{3});

            DbCommand command = GetSqlStringCommand(sqlString);

            //パラメータセット
{4}
            //SQL文を実行する
            return ExecuteDataSet(command, dbManager);
        }}
",
            tableName,
            modelClassName,
            GetSelectFieldsWhere(FieldsDs),
            realTableName,
            GetSelectWhereFieldsBind(FieldsDs));
        }

        private static String GetSelectFieldsWhere(DataSet ds)
        {
            String fields = string.Empty;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                DataRow row = ds.Tables[0].Rows[i];
                String user_type_id = row["user_type_id"].ToString();
                String name = row["name"].ToString();

                if (row["is_pk"].ToString() == "1")
                {
                    if (String.IsNullOrEmpty(fields))
                    {
                        fields += String.Format("                    {0} = @{0}{1}", name, Environment.NewLine);
                    }
                    else
                    {
                        fields += String.Format("                    AND {0} = @{0}{1}", name, Environment.NewLine);
                    }
                }

            }

            fields = fields.TrimEnd(Environment.NewLine.ToCharArray());
            return fields;
        }

        private static String GetSelectWhereFieldsBind(DataSet ds)
        {
            String fields = string.Empty;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                DataRow row = ds.Tables[0].Rows[i];
                String user_type_id = row["user_type_id"].ToString();
                String name = row["name"].ToString();

                String type = "String";
                if (user_type_id == "56")
                {
                    type = "Int32";
                }
                else if (user_type_id == "61" || user_type_id == "40")
                {
                    type = "DateTime";
                }

                if (row["is_pk"].ToString() == "1")
                {
                    fields += String.Format(@"            AddInParameter(command, ""@{0}"", DbType.{1}, model.{0});", name, type) + Environment.NewLine;
                }
            }

            return fields;
        }
    }
}
